//
//  NYExamineViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYExamineViewController.h"
#import "NYExamineRequest.h"
#import "NYExamineSectionHeaderView.h"
#import "NYExamineViewStartTestViewController.h"

@interface NYExamineViewController ()
@property(nonatomic, strong) NSArray <NYExamineListModel *>*listData;
@end

@implementation NYExamineViewController
- (void)didInitializedWithStyle:(UITableViewStyle)style
{
        [super didInitializedWithStyle:style];
}
- (void)viewDidLoad
{
        [super viewDidLoad];
        [self loadData];
}

- (void)loadData
{
        [self setLoading:YES animation:YES];
        
        [[NYExamineRequest requestWithParams:@{
                                               kRequestKeyChannel : @(NYUserManager.channelID)
                                               }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYExamineListModel *> * _Nonnull data) {
                
                self.listData = data;
                [self configNimbusData:data];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)configNimbus
{
        [super configNimbus];
        [self.tableView registerClass:[NYExamineSectionHeaderView class] forHeaderFooterViewReuseIdentifier:[NYExamineSectionHeaderView className]];
        [self.tableView registerClass:[BaseTableHeaderFooterView class] forHeaderFooterViewReuseIdentifier:[BaseTableHeaderFooterView className]];
        @weakify(self);
        [self.tableViewActions attachToClass:[NYExamineModel class] tapBlock:^BOOL(NYExamineModel * object, NYExamineViewController * target, NSIndexPath *indexPath) {
                @strongify(self);
                [NYUserManager pushOrPresentLoginIfNeed:YES inViewController:target completion:^{
                        
                } dismiss:^{
                        if (NYUserManager.isLogin) {
                                NYExamineViewStartTestViewController *vc = [[NYExamineViewStartTestViewController alloc] initWithExamineRequestStatus:NYExamineRequestStatusTest withExamineID:object.id];
                                vc.dismissBlock = ^{
                                        [self loadData];
                                };
                                [target.navigationController pushViewController:vc animated:YES];                                
                        }
                }];
                return YES;
        }];
}
- (void)configNimbusData:(NSArray <NYExamineListModel *>*)data
{
        [self configNimbus];
        [data enumerateObjectsUsingBlock:^(NYExamineListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.tableViewModel addSectionWithTitle:nil];
                obj.sectionIndex = idx;
                /// 初始是否展开试卷
                obj.expend = NO;
//                [self.tableViewModel addObjectsFromArray:obj.examine_list];
        }];
        [self.tableView reloadData];

}
- (void)expandOrClosed:(BOOL)expand For:(NYExamineListModel *)model
{
        if (expand)
        {
                [self.tableViewModel addObjectsFromArray:model.examine_list toSection:model.sectionIndex];
                [self.tableView reloadSection:model.sectionIndex withRowAnimation:(UITableViewRowAnimationAutomatic)];
        }
        else
        {
                [self.tableViewModel removeObjectsInSection:model.sectionIndex];
                [self.tableView reloadSection:model.sectionIndex withRowAnimation:(UITableViewRowAnimationAutomatic)];
        }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
        return __WGHeight(50);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
        return __WGHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        NYExamineSectionHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[NYExamineSectionHeaderView className]];
        header.listModel = [self.listData jk_objectWithIndex:section];
        @weakify(self);
        [header setActionBlock:^(NYExamineListModel *listModel, BOOL expend) {
                @strongify(self);
                [self expandOrClosed:expend For:listModel];
        }];
        return header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
        BaseTableHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[BaseTableHeaderFooterView className]];
        footer.contentView.backgroundColor = UIColorForBackground;
        return footer;
}
+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}
@end
